package com.yowasa.xiuxian.service.helper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.po.XiuXianExtend;
import com.yowasa.xiuxian.entity.po.XiuxianPrayerLog;
import com.yowasa.xiuxian.mapper.XiuxianExtendMapper;
import com.yowasa.xiuxian.mapper.XiuxianPrayerLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class PrayerHelper {

    @Autowired
    private XiuxianPrayerLogMapper xiuxianPrayerLogMapper;


    public int getTimes(UserBO user) {
        XiuxianPrayerLog xiuxianPrayerLog = xiuxianPrayerLogMapper.selectOne(new LambdaQueryWrapper<XiuxianPrayerLog>()
                .eq(XiuxianPrayerLog::getAccountId, user.getId())
                .eq(XiuxianPrayerLog::getType, 1)
                .orderByDesc(XiuxianPrayerLog::getCreateTime)
                .last("LIMIT 1"));

        if(xiuxianPrayerLog == null){
            return xiuxianPrayerLogMapper.selectCount(new LambdaQueryWrapper<XiuxianPrayerLog>()
                    .eq(XiuxianPrayerLog::getAccountId,user.getId())).intValue();
        }else {
            return xiuxianPrayerLogMapper.selectCount(new LambdaQueryWrapper<XiuxianPrayerLog>()
                    .eq(XiuxianPrayerLog::getAccountId,user.getId())
                    .gt(XiuxianPrayerLog::getCreateTime,xiuxianPrayerLog.getCreateTime())).intValue();
        }

    }

    public void insert(UserBO user, String s,Integer type) {
        XiuxianPrayerLog xiuxianPrayerLog = new XiuxianPrayerLog();
        xiuxianPrayerLog.setAccountId(user.getId());
        xiuxianPrayerLog.setCreateTime(new Date());
        xiuxianPrayerLog.setItemName(s);
        xiuxianPrayerLog.setType(type);
        xiuxianPrayerLogMapper.insert(xiuxianPrayerLog);
    }

    public List<XiuxianPrayerLog> selectSize(UserBO user,int i) {
        LambdaQueryWrapper<XiuxianPrayerLog> wrapper = new LambdaQueryWrapper<XiuxianPrayerLog>()
                .eq(XiuxianPrayerLog::getAccountId, user.getId())
                .orderByDesc(XiuxianPrayerLog::getCreateTime);
        Page<XiuxianPrayerLog> page = new Page<>(i,10);

        Page<XiuxianPrayerLog> xiuxianPrayerLogPage = xiuxianPrayerLogMapper.selectPage(page, wrapper);
        List<XiuxianPrayerLog> records = xiuxianPrayerLogPage.getRecords();
        return records;
    }
}
